package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.ParkService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.park.vo.*;
import com.ibigcat.module.mdm.dao.entity.ParkDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.ParkMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 园区管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ParkServiceImpl implements ParkService {

    @Resource
    private ParkMapper parkMapper;

    @Override
    public Long createPark(ParkSaveReqVO createReqVO) {
        // 插入
        ParkDO park = BeanUtils.toBean(createReqVO, ParkDO.class);
        parkMapper.insert(park);
        // 返回
        return park.getId();
    }

    @Override
    public void updatePark(ParkSaveReqVO updateReqVO) {
        // 校验存在
        validateParkExists(updateReqVO.getId());
        // 更新
        ParkDO updateObj = BeanUtils.toBean(updateReqVO, ParkDO.class);
        parkMapper.updateById(updateObj);
    }

    @Override
    public void deletePark(Long id) {
        // 校验存在
        validateParkExists(id);
        // 删除
        parkMapper.deleteById(id);
    }

    private void validateParkExists(Long id) {
        if (parkMapper.selectById(id) == null) {
            throw exception(PARK_NOT_EXISTS);
        }
    }

    @Override
    public ParkDO getPark(Long id) {
        return parkMapper.selectById(id);
    }

    @Override
    public PageResult<ParkDO> getParkPage(ParkPageReqVO pageReqVO) {
        return parkMapper.selectPage(pageReqVO);
    }

}